﻿
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIModel 
{
    private UIModelStudio _modelStudio;
    public UIModel(int width,int height,RawImage targetImage,GameObject modelRoot)
    {
        _modelStudio = new UIModelStudio();
        _modelStudio.Init();
        _modelStudio.SetModel(modelRoot);

        var camera = _modelStudio.Root.Find<Camera>("Camera");
        var texture = new RenderTexture(width, height, 24);
        //texture 要画在图片上
        camera.targetTexture = texture;
        targetImage.texture = camera.targetTexture;
    }
    internal void SetStudioPosition(ref Vector3 pos)
    {
        _modelStudio.Root.transform.position = pos;
    }
    public void Destroy()
    {
        _modelStudio.Destroy(); 
    }
}
//管理所有的UIModel，使得其两两之间不可见
public class UIModelMgr : BaseManager<UIModelMgr>
{
    List<UIModel> _allUIModel = new List<UIModel>();
    private readonly Vector3 _srcPos = new Vector3(-100, 100, 100);
    private readonly float _deltaY = -20;
    private Vector3 _curPos;
    public UIModelMgr()
    {
        _curPos = _srcPos;
    }
    public UIModel CreateUIModel(int width,int height,RawImage targetImage,GameObject uiRoot)
    {
        var uiModel = new UIModel(width,height,targetImage,uiRoot);
        uiModel.SetStudioPosition(ref _curPos);
        _curPos = new Vector3(_curPos.x, _curPos.y+_deltaY, _curPos.z);
        _allUIModel.Add(uiModel);
        return uiModel;
    }
    public void Release(UIModel uiModel)
    {
        uiModel.Destroy();
        _allUIModel.Remove(uiModel);
    }
}
